import os
import cv2
import numpy as np
import time
import datetime
from PIL import Image

def image_Splicing(img_1, img_2, flag='y'):
    img1 = Image.fromarray(cv2.cvtColor(img_1, cv2.COLOR_BGR2RGB))
    img2 = Image.fromarray(cv2.cvtColor(img_2, cv2.COLOR_BGR2RGB))
    size1, size2 = img1.size, img2.size
    if flag == 'x':
        joint = Image.new("RGB", (size1[0] + size2[0], size1[1]))
        loc1, loc2 = (0, 0), (size1[0], 0)
    else:
        joint = Image.new("RGB", (size1[0], size2[1]+size1[1]))
        loc1, loc2 = (0, 0), (0, size1[1])
    joint.paste(img1, loc1)
    joint.paste(img2, loc2)
    # joint.save('joint.png')
    return cv2.cvtColor(np.asarray(joint), cv2.COLOR_RGB2BGR)


def printTime():
    print(datetime.datetime.now().strftime('%Y:%m:%d %H:%M:%S.%f'))
    return

filename = r'E:\labelAnnotation\Screenshots\WoWScrnShot_073022_093258.jpg'
image = cv2.imread(filename)
# cv2.imshow("img",image)
# cv2.waitKey(0)

gray1 = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
gray2 = cv2.medianBlur(gray1,3)
gray3 = cv2.GaussianBlur(gray2,(3,3),0)
edges = cv2.Canny(gray3,50,300)

all1 = image_Splicing(image,edges,'y')

min_line_length = 500
max_line_gap = 40
 
# lines = cv2.HoughLinesP(edges, 2, np.pi/180, 20, min_line_length, max_line_gap)
# for line in lines:
#     for x1, y1, x2, y2 in line:
#         cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
#         # print('y1' + str(y1))
#         # print('y2' + str(y2))

all2 = image_Splicing(all1,image,'x')
all3 = image_Splicing(all2,find_squares(image),'x')
cv2.namedWindow('test', cv2.WINDOW_NORMAL)
cv2.resizeWindow('test',1000, 1000)
cv2.imshow('test',all2)
cv2.waitKey(0)

cv2.imwrite('yehaibo.jpg',all2)